package org.shj.basic;

import java.util.concurrent.TimeUnit;


public class JoinDemo {

	public static void main(String[] args) throws Exception{
		Thread previous = Thread.currentThread();
		for(int i = 0 ; i < 10; i++){
			//每一个线程拥有前一个线程的引用，需要等待前一个线程终止，才能从等待中返回
			Thread thread = new Thread(new Domino(previous), String.valueOf(i));
			thread.start();
			previous = thread;
		}
		
		System.out.println(Thread.currentThread().getName() + " will terminate in 5 seconds.");
		TimeUnit.SECONDS.sleep(5);
	}
	
	static class Domino implements Runnable{
		private Thread thread;
		public Domino(Thread thread){
			this.thread = thread;
		}
		
		public void run(){
			try{
				thread.join();
			}catch(InterruptedException e){
				
			}
			System.out.println(Thread.currentThread().getName() + " terminate");
		}
	}
}
